package com.itheima.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.itheima.interceptor.AuthInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    @Autowired
    RedisTemplate<Object, Object>  redisTemplate;


    /**
     * 设置静态资源映射
     * @param registry
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("开始进行静态资源映射...");
        registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/backend/");
        registry.addResourceHandler("/front/**").addResourceLocations("classpath:/front/");
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new AuthInterceptor(redisTemplate))
                .addPathPatterns(
                        "/employee/**",
                        "/order/**",
                        "/dish/**",
                        "/setmeal/**",
                        "/category/**"
                )
                .excludePathPatterns("/employee/login");


        super.addInterceptors(registry);
    }


    //添加转换器
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(fastJsonHttpMessageConverters());
    }

    @Bean//使用@Bean注入fastJsonHttpMessageConvert
    public HttpMessageConverter fastJsonHttpMessageConverters(){
                  //1.需要定义一个Convert转换消息的对象
                FastJsonHttpMessageConverter fastConverter=new FastJsonHttpMessageConverter();
                //2.添加fastjson的配置信息，比如是否要格式化返回的json数据
            //
                    FastJsonConfig fastJsonConfig=new FastJsonConfig();
                    fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
                    fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
                    //3.在convert中添加配置信息
                    fastConverter.setFastJsonConfig(fastJsonConfig);
                    HttpMessageConverter<?> converter=fastConverter;

                    List<MediaType> supportedMediaTypes = new ArrayList<>();

                    supportedMediaTypes.add(MediaType.APPLICATION_JSON);
                    supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
                    supportedMediaTypes.add(MediaType.APPLICATION_ATOM_XML);
                    supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
                    supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
                    supportedMediaTypes.add(MediaType.APPLICATION_PDF);
                    supportedMediaTypes.add(MediaType.APPLICATION_RSS_XML);
                    supportedMediaTypes.add(MediaType.APPLICATION_XHTML_XML);
                    supportedMediaTypes.add(MediaType.APPLICATION_XML);
                    supportedMediaTypes.add(MediaType.IMAGE_GIF);
                    supportedMediaTypes.add(MediaType.IMAGE_JPEG);
                    supportedMediaTypes.add(MediaType.IMAGE_PNG);
                    supportedMediaTypes.add(MediaType.TEXT_EVENT_STREAM);
                    supportedMediaTypes.add(MediaType.TEXT_HTML);
                    supportedMediaTypes.add(MediaType.TEXT_MARKDOWN);
                    supportedMediaTypes.add(MediaType.TEXT_PLAIN);
                    supportedMediaTypes.add(MediaType.TEXT_XML);
                    fastConverter.setSupportedMediaTypes(supportedMediaTypes);

                    return converter;
     }




}
